//
//  PingLunDetailCon.m
//  ShopDemo
//
//  Created by 飞扬的青春 on 2018/8/3.
//  Copyright © 2018年 飞扬的青春. All rights reserved.
//

#import "PingLunDetailCon.h"
#import "LoginController.h"



@interface PingLunDetailCon ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>




@property (strong, nonatomic)UITableView *listTableview;
@property(nonatomic,strong)NSArray *contentArray;
@property(nonatomic,strong)UITextField *plfield;
@property(nonatomic,strong)NSString *rateId;
@property(nonatomic,strong)NSString *rateUserId;
@property(nonatomic,strong)UIView *backview;


@end

@implementation PingLunDetailCon

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *strurl = [NSString stringWithFormat:@"/dashop/newsrate/app/list.do?NEWS_ID=%@&CurrentPage=1&NEWSRATE_PARENTID=%@",self.maindic[@"NEWS_ID"],self.maindic[@"NEWSRATE_ID"]];
    [self contentDataMethodWithUrl:[MethodClass returnUrlSetStrWSDLURL:strurl]];
    [self createTableviewMethod];
    [self bottomViewMethod];
    [self creatHeaderMehtod];
    
    self.rateId = self.maindic[@"NEWSRATE_ID"];
    self.rateUserId = self.maindic[@"NEWSRATE_USERID"];
    
    UIBarButtonItem *btn = [[UIBarButtonItem  alloc]initWithTitle:@"举报" style:UIBarButtonItemStylePlain target:self action:@selector(JuBaoButtonAction:)];
    self.navigationItem.rightBarButtonItem = btn;
    [self creatBackViewMethod];
}


-(void)JuBaoButtonAction:(UIBarButtonItem *)item{
    self.backview.hidden = NO;
    //返回封装好的编辑框
    UIView *bigview = [self returnBigTextviewMetodUserDic:nil];
    [self.backview addSubview:bigview];
    bigview.sd_layout
    .topSpaceToView(self.backview, 100)
    .leftSpaceToView(self.backview, 30)
    .rightSpaceToView(self.backview,30)
    .heightIs(kScreenHeight/3);
}


//添加主背景页面和设置背景页面
-(void)creatBackViewMethod{
    //添加主背景view
    self.backview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    self.backview.backgroundColor = [[UIColor darkGrayColor] colorWithAlphaComponent:0.5];
    [self.navigationController.view addSubview:self.backview];
    self.backview.userInteractionEnabled = YES;
    self.backview.hidden = YES;
    
}


//返回大的编辑框
-(UIView *)returnBigTextviewMetodUserDic:(NSDictionary *)dic{
    UIView *bigview = [UIView new];
    bigview.layer.masksToBounds = YES;
    bigview.layer.cornerRadius = 5;
    bigview.backgroundColor = LineBackColor;
    UILabel *titLab = [UILabel new];
    titLab.font = [UIFont systemFontOfSize:15];
    titLab.textColor = [UIColor blackColor];
    titLab.textAlignment = NSTextAlignmentCenter;
    titLab.text = @"举报内容";
    [bigview addSubview:titLab];
    titLab.sd_layout
    .topSpaceToView(bigview, 0)
    .leftSpaceToView(bigview, 0)
    .rightSpaceToView(bigview, 0)
    .heightIs(30);
    UITextView *textview = [UITextView new];
    textview.tag = 603;
    textview.layer.masksToBounds = YES;
    textview.layer.cornerRadius = 5;
    textview.backgroundColor = [UIColor whiteColor];
    [bigview addSubview:textview];
    textview.sd_layout
    .topSpaceToView(titLab, 5)
    .leftSpaceToView(bigview, 10)
    .rightSpaceToView(bigview, 10)
    .bottomSpaceToView(bigview, 50);
    NSInteger btnWidth = (kScreenWidth-60-40)/2;
    MineButton *cancelBtn = [MineButton buttonWithType:UIButtonTypeSystem];
    cancelBtn.tag = 600;
    cancelBtn.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.3];
    [cancelBtn addTarget:self action:@selector(TextButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:LineBackColor forState:UIControlStateNormal];
    [bigview addSubview:cancelBtn];
    cancelBtn.sd_layout
    .topSpaceToView(textview, 10)
    .leftSpaceToView(bigview, 10)
    .bottomSpaceToView(bigview, 10)
    .widthIs(btnWidth);
    MyButton *okBtn = [MyButton buttonWithType:UIButtonTypeSystem];
    [okBtn addTarget:self action:@selector(TextButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    okBtn.tag = 601;
    okBtn.backgroundColor = BackColor;
    [okBtn setTitle:@"确认" forState:UIControlStateNormal];
    [okBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [bigview addSubview:okBtn];
    okBtn.sd_layout
    .topSpaceToView(textview, 10)
    .rightSpaceToView(bigview, 10)
    .bottomEqualToView(cancelBtn)
    .widthIs(btnWidth);
    return bigview;
}

-(void)TextButtonAction:(MineButton *)sender{
    UITextView *textview = [self.backview viewWithTag:603];
    if (sender.tag == 601) {
        if (textview.text.length > 0) {
            NSString *urlstr = [NSString stringWithFormat:@"/dashop/news/app/tipOffs.do?NEWS_ID=%@&NEWS_TIPOFFS=%@",self.maindic[@"NEWS_ID"],textview.text];
            NSString *endstr = [MethodClass returnUrlSetStrWSDLURL:urlstr];
            NSString  *newUrlString = [endstr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
            [self JuBaoRequestMethodWithUrl:newUrlString];
        }else{
            [self alerWithMessage:@"请填写举报内容"];
        }
    }else{
        self.backview.hidden = YES;
        [self.backview.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }
}
//举报
-(void)JuBaoRequestMethodWithUrl:(NSString *)url{
    [YQNetworking getWithUrl:url refreshRequest:YES cache:NO params:nil progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
    } successBlock:^(id response) {
        NSDictionary *dic = response;
        if ([dic[@"RESULT"] integerValue] == 1) {
            [[NSOperationQueue mainQueue]addOperationWithBlock:^{
                [self alerWithMessage:dic[@"MESSAGE"]];
                self.backview.hidden = YES;
                [self.backview.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
            }];
        }
    } failBlock:^(NSError *error) {
        
    }];
    
}






-(void)bottomViewMethod{
    UIView *bottomview = [UIView new];
    bottomview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomview];
    bottomview.sd_layout
    .bottomSpaceToView(self.view,0)
    .centerXEqualToView(self.view)
    .widthIs(kScreenWidth)
    .heightIs(60*kJLXWidthScale);
    
    UITextField *field = [UITextField new];
    self.plfield = field;
    field.layer.masksToBounds = YES;
    field.layer.cornerRadius = 5*kJLXWidthScale;
    field.layer.borderColor = [UIColor orangeColor].CGColor;
    field.layer.borderWidth = 1;
    field.placeholder = @"  说点什么……";
    field.delegate = self;
    field.font = [UIFont systemFontOfSize:18*kJLXWidthScale];
    [bottomview addSubview:field];
    field.sd_layout
    .leftSpaceToView(bottomview, 8*kJLXWidthScale)
    .bottomSpaceToView(bottomview, 10*kJLXWidthScale)
    .heightIs(45*kJLXWidthScale)
    .rightSpaceToView(bottomview, 80*kJLXWidthScale);
    
    UILabel *lab = [UILabel new];
    lab.textAlignment = NSTextAlignmentCenter;
    lab.textColor = [UIColor orangeColor];
    lab.text = @"评论";
    lab.font = [UIFont systemFontOfSize:20*kJLXWidthScale];
    [bottomview addSubview:lab];
    lab.sd_layout
    .bottomSpaceToView(bottomview, 10*kJLXWidthScale)
    .rightSpaceToView(bottomview, 10*kJLXWidthScale)
    .leftSpaceToView(field, 10*kJLXWidthScale)
    .heightIs(45*kJLXWidthScale);
    self.listTableview.sd_layout
    .bottomSpaceToView(bottomview, 0);
    MyGestureTap *tap = [[MyGestureTap alloc]initWithTarget:self action:@selector(pinglunTap:)];
    lab.userInteractionEnabled = YES;
    [lab addGestureRecognizer:tap];
    
    self.plfield.placeholder = [NSString stringWithFormat:@"回复：%@",self.maindic[@"NEWSRATE_USERIDNAME"]];
}

-(void)pinglunTap:(MyGestureTap*)tap{
    if (self.plfield && self.plfield.text.length > 0) {
        NSString *strurl = [NSString stringWithFormat:@"/dashop/newsrate/app/add.do?NEWSRATE_USERID=%@&NEWSRATE_USERNAME=%@&NEWSRATE_CONTENT=%@&NEWS_ID=%@&NEWSRATE_PARENTID=%@&NEWSRATE_TOUSERID=%@",[UserDefault objectForKey:LogUserID],[UserDefault objectForKey:LogUserName],self.plfield.text,self.maindic[@"NEWS_ID"],self.rateId,self.rateUserId];
        NSString *endurl = [MethodClass returnUrlSetStrWSDLURL:strurl];
        NSString  *newUrlString = [endurl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        [self addPingLunMethodWithUrl:newUrlString];
    }
    
}




//评论列表
-(void)contentDataMethodWithUrl:(NSString *)url{
    [YQNetworking getWithUrl:url refreshRequest:YES cache:NO params:nil progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
    } successBlock:^(id response) {
       self.contentArray = response;
        NSMutableArray *muarr = [self.contentArray mutableCopy];
        [muarr removeObjectAtIndex:0];
        self.contentArray = [muarr copy];
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
            [self.listTableview reloadData];
        }];
    } failBlock:^(NSError *error) {
        
    }];
    
}

//添加评论
-(void)addPingLunMethodWithUrl:(NSString *)url{
    [YQNetworking getWithUrl:url refreshRequest:YES cache:NO params:nil progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
    } successBlock:^(id response) {
        if ([response[@"RESULT"]integerValue] == 1) {
            NSString *strurl = [NSString stringWithFormat:@"/dashop/newsrate/app/list.do?NEWS_ID=%@&CurrentPage=1&NEWSRATE_PARENTID=%@",self.maindic[@"NEWS_ID"],self.maindic[@"NEWSRATE_ID"]];
            [self contentDataMethodWithUrl:[MethodClass returnUrlSetStrWSDLURL:strurl]];
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                self.plfield.text = @"";
            }];
        }
    } failBlock:^(NSError *error) {
        
    }];
    
}



-(void)creatHeaderMehtod{
    UIImageView *imagevi = [[UIImageView alloc]initWithFrame:CGRectMake(10*kJLXWidthScale, 10*kJLXWidthScale,35*kJLXWidthScale, 35*kJLXWidthScale)];
    imagevi.layer.masksToBounds = YES;
    imagevi.layer.cornerRadius = imagevi.width/2;
    NSString *strurl = [NSString stringWithFormat:@"/dashop/%@",self.maindic[@"SFID"]];
    NSString *endstr = [MethodClass returnUrlSetStrWSDLURL:strurl];
    [imagevi sd_setImageWithURL:[NSURL URLWithString:endstr]];
    [self.view addSubview:imagevi];
    UILabel *lab = [UILabel new];
    lab.textColor = BackColor;
    lab.text = [NSString stringWithFormat:@"%@",self.maindic[@"NEWSRATE_USERIDNAME"]];
    lab.font = [UIFont systemFontOfSize:16*kJLXWidthScale];
    [self.view addSubview:lab];
    lab.sd_layout
    .topSpaceToView(self.view,10*kJLXWidthScale)
    .leftSpaceToView(imagevi, 5*kJLXWidthScale)
    .heightIs(25*kJLXWidthScale)
    .rightSpaceToView(self.view, 10*kJLXWidthScale);
    
    UILabel *twolab = [UILabel new];
    twolab.font = [UIFont systemFontOfSize:13*kJLXWidthScale];
    twolab.textColor = [[UIColor grayColor]colorWithAlphaComponent:0.6];
    [self.view addSubview:twolab];
    twolab.sd_layout
    .topSpaceToView(lab, 0)
    .leftSpaceToView(imagevi, 5*kJLXWidthScale)
    .rightSpaceToView(self.view, 0)
    .heightIs(10*kJLXWidthScale);
    NSString *contentStr = [NSString stringWithFormat:@"%@",self.maindic[@"NEWSRATE_CONTENT"]];
    CGRect rect = [contentStr boundingRectWithSize:CGSizeMake(kScreenWidth, MAXFLOAT) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17*kJLXWidthScale]} context:nil];
    UILabel *contlab = [UILabel new];
    contlab.text = contentStr;
    contlab.font = [UIFont systemFontOfSize:15*kJLXWidthScale];
    contlab.numberOfLines = 0;
    [self.view addSubview:contlab];
    contlab.sd_layout
    .topSpaceToView(imagevi, 5*kJLXWidthScale)
    .leftSpaceToView(self.view, 10*kJLXWidthScale)
    .rightSpaceToView(self.view, 10*kJLXWidthScale)
    .heightIs(rect.size.height);
    
}
-(void)createTableviewMethod{
    NSString *contentStr = [NSString stringWithFormat:@"%@",self.maindic[@"NEWSRATE_CONTENT"]];
    CGRect rect = [contentStr boundingRectWithSize:CGSizeMake(kScreenWidth, MAXFLOAT) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17*kJLXWidthScale]} context:nil];
    self.listTableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 70*kJLXWidthScale + rect.size.height, kScreenWidth, kScreenHeight - (70*kJLXWidthScale + rect.size.height)-120*kJLXWidthScale) style:UITableViewStylePlain];
    [self.view addSubview:self.listTableview];
    self.listTableview.dataSource = self;
    self.listTableview.delegate = self;
    [self.listTableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"onecell"];
    //分隔线
    self.listTableview.separatorStyle = UITableViewCellAccessoryNone;
    //去掉尾部多余的空白
    [self.listTableview setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    
}

#pragma mark --UITableViewDelegate,UITableViewDataSource--
//-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//    return 2;
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.contentArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"onecell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *dic = self.contentArray[indexPath.row];
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    NSString *str1 = [NSString stringWithFormat:@"%@",dic[@"NEWSRATE_USERIDNAME"]];
    UILabel *onelab = [UILabel new];
    onelab.textColor = BackColor;
    onelab.numberOfLines = 0;
    onelab.font = [UIFont systemFontOfSize:18*kJLXWidthScale];
    onelab.textAlignment = NSTextAlignmentLeft;
    [cell.contentView addSubview:onelab];
    onelab.sd_layout
    .topSpaceToView(cell.contentView, 8*kJLXWidthScale)
    .leftSpaceToView(cell.contentView, 8*kJLXWidthScale)
    .widthIs(kScreenWidth)
    .heightIs(30*kJLXWidthScale);
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:18*kJLXWidthScale];
    CGSize size =  [str1 boundingRectWithSize:CGSizeMake(onelab.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
    onelab.sd_layout
    .topSpaceToView(cell.contentView, 8*kJLXWidthScale)
    .leftSpaceToView(cell.contentView, 8*kJLXWidthScale)
    .widthIs(size.width)
    .heightIs(30*kJLXWidthScale);
    onelab.text = str1;
    
    onelab.userInteractionEnabled = YES;
    MyGestureTap *tap1 = [[MyGestureTap alloc]initWithTarget:self action:@selector(onelabTap:)];
    [onelab addGestureRecognizer:tap1];
    tap1.userInfo = dic;
    
    
    
    UILabel *titlab = [UILabel new];
    titlab.textColor = [UIColor darkGrayColor];
    titlab.text = [NSString stringWithFormat:@"回复"];
    titlab.font = [UIFont systemFontOfSize:16*kJLXWidthScale];
    titlab.textColor = [UIColor blackColor];
    [cell.contentView addSubview:titlab];
    titlab.sd_layout
    .topSpaceToView(cell.contentView, 8*kJLXWidthScale)
    .leftSpaceToView(onelab, 5*kJLXWidthScale)
    .widthIs(45*kJLXWidthScale)
    .heightIs(30*kJLXWidthScale);
    
    NSString *str = [NSString stringWithFormat:@"@%@",dic[@"NEWSRATE_TOUSERIDNAME"]];
    UILabel *twolab = [UILabel new];
    twolab.textColor = BackColor;
    twolab.numberOfLines = 0;
    twolab.font = [UIFont systemFontOfSize:18*kJLXWidthScale];
    twolab.textAlignment = NSTextAlignmentLeft;
    [cell.contentView addSubview:twolab];
    twolab.sd_layout
    .topSpaceToView(cell.contentView, 8*kJLXWidthScale)
    .leftSpaceToView(titlab, 5*kJLXWidthScale)
    .widthIs(kScreenWidth)
    .heightIs(30*kJLXWidthScale);
    
    NSMutableDictionary *attrs1 = [NSMutableDictionary dictionary];
    attrs1[NSFontAttributeName] = [UIFont systemFontOfSize:18*kJLXWidthScale];
    CGSize size1 =  [str boundingRectWithSize:CGSizeMake(twolab.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs1 context:nil].size;
    twolab.sd_layout
    .topSpaceToView(cell.contentView, 8*kJLXWidthScale)
    .leftSpaceToView(titlab,0)
    .widthIs(size1.width)
    .heightIs(30*kJLXWidthScale);
    twolab.text = str;
    twolab.userInteractionEnabled = YES;
    MyGestureTap *tap2 = [[MyGestureTap alloc]initWithTarget:self action:@selector(twolabTap:)];
    tap2.userInfo = dic;
    [twolab addGestureRecognizer:tap2];
    
    
    
    
    UILabel *contlab = [UILabel new];
    contlab.text = [NSString stringWithFormat:@"%@",dic[@"NEWSRATE_CONTENT"]];
    contlab.font = [UIFont systemFontOfSize:15*kJLXWidthScale];
    [cell.contentView addSubview:contlab];
    contlab.sd_layout
    .topSpaceToView(onelab, 0)
    .leftSpaceToView(cell.contentView, 8*kJLXWidthScale)
    .rightSpaceToView(cell.contentView, 8*kJLXWidthScale)
    .bottomSpaceToView(cell.contentView, 8*kJLXWidthScale);
    
    
    
    return cell;
    
    
    
    
}

-(void)onelabTap:(MyGestureTap*)tap{
    
    self.rateId = tap.userInfo[@"NEWSRATE_PARENTID"];
    self.rateUserId = tap.userInfo[@"NEWSRATE_USERID"];
    self.plfield.placeholder = [NSString stringWithFormat:@"回复：%@",tap.userInfo[@"NEWSRATE_USERIDNAME"]];
   
}
-(void)twolabTap:(MyGestureTap *)tap{
    self.rateId = tap.userInfo[@"NEWSRATE_PARENTID"];
    self.rateUserId = tap.userInfo[@"NEWSRATE_TOUSERID"];
    self.plfield.placeholder = [NSString stringWithFormat:@"回复：%@",tap.userInfo[@"NEWSRATE_TOUSERIDNAME"]];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = self.contentArray[indexPath.row];
     NSString *contentStr = [NSString stringWithFormat:@"%@",dic[@"NEWSRATE_CONTENT"]];
    CGRect rect = [contentStr boundingRectWithSize:CGSizeMake(tableView.bounds.size.width, MAXFLOAT) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];
    return rect.size.height+60*kJLXWidthScale;
    
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
}


//是否支持编辑
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    NSString *username = [UserDefault objectForKey:LogUserName];
    if (username && username.length > 0) {
        
    }else{
        LoginController *loginVC = [[LoginController alloc]init];
        loginVC.logBlock = ^(NSDictionary *logdic) {
            
        };
        loginVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:loginVC animated:YES];
    }
    return YES;//YES可以编辑 NO不可编辑
}

//封装提示框方法
-(void)alerWithMessage:(NSString *)msg{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alert animated:YES completion:^{
        //延迟1秒自动消失
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [alert dismissViewControllerAnimated:YES completion:nil];
        });
    }];
}

@end
